package com.thinkgem.jeesite.frame.pm.utils;

import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.frame.pm.entity.PmParam;
import com.thinkgem.jeesite.frame.pm.service.PmParamService;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author:Kongxiangsheng
 * @DesCription:
 * @Date:Created in 18:33 2018/9/17
 * @Modified By:
 */
public class PmParamUtils {
    private static PmParamService pmParamService = SpringContextHolder.getBean(PmParamService.class);

    /**
     * 根据code获取参数信息
     *
     * @param paramCode
     * @return
     */
    public static PmParam getByCode(String paramCode) {
        return pmParamService.getByCode(paramCode);
    }

    /**
     * 获取参数值-获取失败报错
     *
     * @param paramCode
     * @return
     */
    public static String getParamValue(String paramCode) {
        PmParam pmParam = pmParamService.getByCode(paramCode);
        if (null == pmParam || StringUtils.isBlank(pmParam.getValue())) {
            throw new ServiceException("获取参数[" + paramCode + "]信息失败！");
        }
        return pmParam.getValue();
    }

    /**
     * 获取参数值-获取失败返回空
     *
     * @param paramCode
     * @return
     */
    public static String getPmParamValue(String paramCode) {
        PmParam pmParam = pmParamService.getByCode(paramCode);
        if (null == pmParam || StringUtils.isBlank(pmParam.getValue())) {
            return "";
        }
        return pmParam.getValue();
    }
}
